package kpanelhandler;

import java.util.Hashtable;

import javax.swing.JPanel;

/**
 * A panel that is managed by a KPanelHandler.
 * 
 * <p>
 * Last version: February 2, 2013
 * <p>
 * 
 * @author Matias Varela
 *         <p>
 *         Student of Licenciatura en Ciencias de la Computacion, at the
 *         Universidad Nacional del Sur (Argentina).
 *         <p>
 *         project site: http://code.google.com/p/kpanelhandler-project/
 * 
 */
@SuppressWarnings("serial")
public class KPanel extends JPanel {

	// the action commands defined for this panel.
	protected Hashtable<String, KActionCommand> commands;
	// the handler of this panel.
	protected KPanelHandler handler;
	// the unique key for this panel.
	protected String key;


	/**
	 * Constructs a new KPanel identified by a unique <code>key</code>.
	 * 
	 * @param key
	 *            The unique key.
	 */
	public KPanel(String key) {
		super();

		this.key = key;
		this.commands = new Hashtable<String, KActionCommand>();
	}


	/* <<< GETTERS >>> */

	/**
	 * Returns the unique key of this panel.
	 * 
	 * @return The unique key of this panel.
	 */
	public String getKey() {
		return key;
	}


	/**
	 * Returns the handler of this panel.
	 * 
	 * @return the handler of this panel.
	 */
	public KPanelHandler getHandler() {
		return handler;
	}


	/**
	 * Returns whether the action command with <code>key</code> exists.
	 * 
	 * @param key
	 *            action command unique key.
	 * @return whether the action command with <code>key</code> exists.
	 */
	public boolean containsActionCommand(String key) {
		return commands.containsKey(key);
	}


	/**
	 * Returns the action command associated with <code>key</code>.
	 * 
	 * @param key
	 *            The key associated with an action command defined for this
	 *            panel.
	 * @return the action command associated with <b>key</b>.
	 */
	public KActionCommand getActionCommand(String key) {
		return commands.get(key);
	}


	/* <<< COMMANDS >>> */
	/**
	 * Add a new action command to this panel.
	 * 
	 * @param key
	 *            A unique key associated with the action command to be added.
	 * @param actionCommand
	 *            The new action command to be added.
	 */
	public void addActionCommand(String key, KActionCommand actionCommand) {
		commands.put(key, actionCommand);
	}


	/**
	 * Remove the action command associated with <code>key</code>.
	 * 
	 * @param key
	 *            The unique key associated with the action command to be
	 *            removed.
	 * @return Whether the operation has succeed. If the action command
	 *         associated with <code>key</code> does not exist then return
	 *         <code>false</code>; <code>true</code> otherwise.
	 */
	public boolean removeActionCommand(String key) {
		return commands.remove(key) != null;
	}


	/**
	 * Set a new handler.
	 * 
	 * @param handler
	 *            The new handler of this panel.
	 */
	protected void setHandler(KPanelHandler handler) {
		this.handler = handler;
	}
}
